﻿using Microsoft.Extensions.DependencyInjection;

using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace Nereus.xUnit
{

    public class NereusFacts
    {
      

        public class NereusConfiguration : INBuilderConfigure
        {
            public string[] AssemblyPathArray { get; set; }

            public string Title { get; set; }

            public string Content { get; set; }
        }

            
        [Fact]
        public void NereusTest()
        {

            IServiceCollection services = new ServiceCollection().AddNereus(new NereusConfiguration()
            {
                AssemblyPathArray = new string[] { "Nereus.xUnit" },
                Content = "请使用HTTP POST JSON格式进行请求。",
                Title = "接口-API 详细接口文档  by Nereus"
            });
           services.AddSingleton<ExampleInterface, ExampleClass>();
           
            IServiceProvider serviceProvider = services.BuildServiceProvider();
            var _nBuilder = serviceProvider.GetRequiredService<ExampleInterface>().Build();
            var nClassContent = _nBuilder.GetEnumerator();
            while (nClassContent.MoveNext())
            {
                Assert.Equal("ExampleClass", nClassContent.Current.Code);
                Assert.Equal("example class", nClassContent.Current.Description);
                Assert.Equal("举例的类", nClassContent.Current.Name);
                var nMethodContent = nClassContent.Current.Methods.GetEnumerator();
                while(nMethodContent.MoveNext())
                {

                    Assert.Equal("NdTest", nMethodContent.Current.ActionCode);
                    Assert.Equal("Nereus单元测试", nMethodContent.Current.Name);
                    Assert.Equal("Nereus测试", nMethodContent.Current.Description);
                    Assert.Equal("POST", nMethodContent.Current.Method);
                    Assert.Equal("nereus/nd/test", nMethodContent.Current.Uri);
                    var nArgumentPropertyContent = nMethodContent.Current.ArgumentProperties.GetEnumerator();
                    while(nArgumentPropertyContent.MoveNext())
                    {
                        Assert.Equal("ArguyA", nArgumentPropertyContent.Current.Name);
                        Assert.Equal("String", nArgumentPropertyContent.Current.Type);
                        Assert.Equal("参数A", nArgumentPropertyContent.Current.Description);
                    }
                    var nResultProperties = nMethodContent.Current.ResultProperties.GetEnumerator();
                    while (nResultProperties.MoveNext())
                    {
                        if(nResultProperties.Current.Description == "响应A")
                        {
                            Assert.Equal("ResultA", nResultProperties.Current.Name);
                            Assert.Equal("String", nResultProperties.Current.Type);
                            Assert.Equal("响应A", nResultProperties.Current.Description);
                        }
                        if (nResultProperties.Current.Description == "响应B")
                        {
                            Assert.Equal("ResultB", nResultProperties.Current.Name);
                            Assert.Equal("PropertyTest", nResultProperties.Current.Type);
                            Assert.Equal("响应B", nResultProperties.Current.Description);
                            //var nPropertyTestProperties = nResultProperties.Current.;
                        }
                    }
                }          
            }
            Assert.Equal(new string[] { "Nereus.xUnit" },serviceProvider.GetRequiredService<INBuilderConfigure>().AssemblyPathArray);
            Assert.Equal("请使用HTTP POST JSON格式进行请求。", serviceProvider.GetRequiredService<INBuilderConfigure>().Content);
            Assert.Equal("接口-API 详细接口文档  by Nereus", serviceProvider.GetRequiredService<INBuilderConfigure>().Title);

        }

    }
}
